package com.ntanh.mealplanner.main.dao;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.main.objects.Ingredient;
import com.ntanh.mealplanner.main.objects.Recipe;
import com.ntanh.mealplanner.main.objects.WeekMeal;

import java.text.ParseException;
import java.util.ArrayList;

/**
 * Created by Anh Nguyen on 2/27/2015.
 */
public class RecipeDao extends BaseDao {

    public RecipeDao(Context context) {
        super(context);
    }


    public ArrayList<Recipe> getAll(String author, boolean lazy) {
        ArrayList<Recipe> recipes = new ArrayList<Recipe>();
        IngredientDao ingredientDao = new IngredientDao(mContext);
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.RECIPE_TABLE;

        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            Recipe recipe = convertFromCursor(cursor,author,ingredientDao,lazy);
            recipes.add(recipe);
        }
        return recipes;
    }

    public Recipe getById(String author, Integer id) {
        IngredientDao ingredientDao = new IngredientDao(mContext);
        Recipe recipe = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.RECIPE_TABLE
                + " WHERE id = " + id;
        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            recipe = convertFromCursor(cursor,author,ingredientDao,false);
        }
        return recipe;
    }

    public Recipe getByName(String author, String name) {
        IngredientDao ingredientDao = new IngredientDao(mContext);
        Recipe recipe = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.RECIPE_TABLE
                + " WHERE " + Recipe.NAME + " = '" + name + "'";
        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            recipe = convertFromCursor(cursor,author,ingredientDao,false);
        }
        return recipe;
    }

    private Recipe convertFromCursor(Cursor cursor, String author, IngredientDao ingredientDao, boolean lazy) {
        Recipe recipe = new Recipe(author);
        recipe.setId(cursor.getInt(0));
        try {
            recipe.setCreatedDate(BaseEntity.dateFormatter.parse(cursor.getString(1)));
        } catch (ParseException e) {
            recipe.setCreatedDate(null);
        }
        try {
            recipe.setModifiedDate(BaseEntity.dateFormatter.parse(cursor.getString(2)));
        } catch (ParseException e) {
            recipe.setModifiedDate(null);
        }
        recipe.setCreatedBy(cursor.getString(3));
        recipe.setModifiedBy(cursor.getString(4));
        recipe.setActive(cursor.getInt(5) == 1);
        recipe.setDeleted(cursor.getInt(6) == 1);
        recipe.setSynced(cursor.getInt(7) == 1);
        recipe.setName(cursor.getString(8));
        recipe.setImage(cursor.getString(9));
        recipe.setDescription(cursor.getString(10));
        if (!lazy) {
            String ingredientsStr = cursor.getString(11);
            if(ingredientsStr != null) {
                if (ingredientsStr.contains(BaseEntity.entitySplitSign)) {
                    String[] array = ingredientsStr.split(BaseEntity.entitySplitSign);
                    for (String ingredientId : array) {
                        if (!ingredientId.isEmpty()) {
                            String[] values = ingredientId.split("-");
                            Ingredient ingredient = ingredientDao.getById(author, Integer.parseInt(values[0]));
                            if (ingredient != null) {
                                recipe.getIngredientList().put(ingredient, Float.parseFloat(values[1]));
                            }
                        }
                    }
                } else if (!ingredientsStr.isEmpty()) {
                    String[] values = ingredientsStr.split("-");
                    Ingredient ingredient = ingredientDao.getById(author, Integer.parseInt(values[0]));
                    if (ingredient != null) {
                        recipe.getIngredientList().put(ingredient, Float.parseFloat(values[1]));
                    }
                }
            }
        }
        recipe.setSourceURL(cursor.getString(12));

        return recipe;
    }
}
